import axios from 'axios';

const instance = axios.create({
  baseURL: '/api', // 与 vite.config.js 中的代理路径匹配
  timeout: 5000 // 设置请求超时时间
});

// 请求拦截器
instance.interceptors.request.use(
  config => {
	if (config.url && config.url.startsWith('/api')) {
	  config.url = config.url.replace('/api', '');
	}
    const token = localStorage.getItem('user')
     ? JSON.parse(localStorage.getItem('user')).token
      : null;
	const shangken = localStorage.getItem('wushang')
	 ? JSON.parse(localStorage.getItem('wushang')).token
	  : null;
	  
    if (token) {
      config.headers['token'] = `${token}`;
    }
	if (shangken) {
	  config.headers['wushang'] = `${shangken}`;
	}
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

export default instance;